-
Notifications
You must be signed in to change notification settings - Fork 0
/
CardPageDisplay.cs
202 lines (177 loc) · 7 KB
/
CardPageDisplay.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class CardPageDisplay : MonoBehaviour
{
[Header("IS_SCROLL_PAGE")]
[SerializeField] private bool isScrollPage;
[Header("PREFABS")]
[SerializeField] private GameObject cardPageCardContainerPrefab;
[SerializeField] private GameObject cardShopButtonPrefab;
[Header("REFERENCES")]
[SerializeField] private GameObject cardGroup;
[SerializeField] private GameObject costGroup, pageTitle, noCardsTooltip;
[Header("PROGRESS BAR")]
[SerializeField] private GameObject progressBar;
[SerializeField] private GameObject progressFill, progressBarText;
private Scrollbar scrollbar;
private List<Card> cardGroupList;
private CardPageType cardPageType;
public enum CardPageType
{
RemoveCard,
RecruitUnit,
AcquireAction,
CloneUnit,
}
public bool IsScrollPage { get => isScrollPage; }
private void Awake()
{
if (isScrollPage)
{
scrollbar = GetComponentInChildren<Scrollbar>();
if (scrollbar == null) Debug.LogError("SCROLLBAR IS NULL!");
}
}
private void Update()
{
if (!isScrollPage) return;
float newValue = scrollbar.value + Input.mouseScrollDelta.y * 0.05f;
if (newValue > 1) newValue = 1;
else if (newValue < 0) newValue = 0;
scrollbar.value = newValue;
}
public void SetProgressBar(int currentProgress, int newProgress, bool isReady, bool isFirstDisplay = false)
{
int goal;
string endText;
switch (cardPageType)
{
case CardPageType.RecruitUnit:
goal = GameManager.RECRUIT_LOYALTY_GOAL;
endText = "Units Recruited";
break;
case CardPageType.AcquireAction:
goal = GameManager.ACTION_LOYALTY_GOAL;
endText = "Actions Acquired";
break;
default:
Debug.LogError("INVALID TYPE!");
return;
}
string progressText = isReady ? "DISCOUNT APPLIED!" : newProgress + "/" + goal + " " + endText;
progressBarText.GetComponent<TextMeshProUGUI>().SetText(progressText);
if (!(isFirstDisplay && newProgress < 1))
Managers.AN_MAN.SetProgressBar(currentProgress, newProgress, progressBar, progressFill);
}
public void DisplayCardPage(CardPageType cardPageType, bool isReload, float scrollValue)
{
this.cardPageType = cardPageType;
cardGroupList = new();
string titleText;
bool setProgressBar = false;
int progress = 0;
switch (cardPageType)
{
case CardPageType.RemoveCard:
titleText = "Sell a Card";
foreach (Card c in Managers.P_MAN.DeckList)
cardGroupList.Add(c);
break;
case CardPageType.RecruitUnit:
setProgressBar = true;
progress = Managers.G_MAN.RecruitLoyalty;
titleText = "Recruit a Unit";
foreach (Card c in Managers.CA_MAN.PlayerRecruitUnits)
cardGroupList.Add(c);
break;
case CardPageType.AcquireAction:
setProgressBar = true;
progress = Managers.G_MAN.ActionShopLoyalty;
titleText = "Acquire an Action";
foreach (Card c in Managers.CA_MAN.ActionShopCards)
cardGroupList.Add(c);
break;
case CardPageType.CloneUnit:
titleText = "Clone a Unit";
foreach (Card c in Managers.P_MAN.DeckList)
if (c is UnitCard) cardGroupList.Add(c);
break;
default:
Debug.LogError("INVALID TYPE!");
return;
}
if (progressBar != null)
{
bool isReady = progress == GameManager.SHOP_LOYALTY_GOAL;
progressBar.SetActive(setProgressBar);
if (setProgressBar) SetProgressBar(0, progress, isReady, true);
}
pageTitle.GetComponent<TextMeshProUGUI>().SetText(titleText);
if (cardGroupList.Count > 0)
{
noCardsTooltip.SetActive(false);
cardGroupList = cardGroupList.OrderBy(c => c.StartEnergyCost)
.ThenBy(c => c.CardName)
.ToList();
}
else noCardsTooltip.SetActive(true);
if (isScrollPage) LoadScrollPage(scrollValue);
else LoadCardPage();
if (!isReload) Managers.AU_MAN.StartStopSound("SFX_CreatePopup1");
}
private void LoadScrollPage(float scrollValue)
{
var rect = cardGroup.GetComponent<RectTransform>().rect;
int rows = Mathf.CeilToInt(cardGroupList.Count / 4f);
if (rows < 1) rows = 1;
float height = 650 * rows + 100;
cardGroup.GetComponent<RectTransform>().sizeDelta = new Vector2(rect.width, height);
GetComponentInChildren<ScrollRect>().verticalNormalizedPosition = scrollValue;
foreach (Card card in cardGroupList)
{
var container = Instantiate(cardPageCardContainerPrefab, cardGroup.transform);
var cpccd = container.GetComponent<CardPageCardContainerDisplay>();
var cardPageCard = Managers.CA_MAN.ShowCard(card, new Vector2(), CardManager.DisplayType.Cardpage);
var cd = cardPageCard.GetComponent<CardDisplay>();
cd.DisableVisuals();
cardPageCard.transform.localScale = new Vector2(4, 4);
cardPageCard.transform.SetParent(cpccd.CardPageCard.transform, false);
cardPageCard.transform.localPosition = new Vector2(); // TESTING
CreateCardPageButton(card, cpccd.CardCostButton);
}
}
private void LoadCardPage()
{
if (cardGroupList.Count > 4)
{
Debug.LogError("MORE THAN 4 CARDS ON CARD PAGE!");
return;
}
foreach (var card in cardGroupList)
{
var cardObj = Managers.CA_MAN.ShowCard(card, new Vector2(), CardManager.DisplayType.Cardpage);
cardObj.transform.SetParent(cardGroup.transform);
var cd = cardObj.GetComponent<CardDisplay>();
cd.DisableVisuals();
cardObj.transform.localScale = new Vector2(4, 4);
var button = CreateCardPageButton(card, costGroup);
if (button == null) return;
}
}
private GameObject CreateCardPageButton(Card card, GameObject parent)
{
var button = Instantiate(cardShopButtonPrefab, parent.transform);
button.GetComponent<CardShopButton>().SetCard(card, cardPageType);
return button;
}
public void CloseCardPageButton_OnClick()
{
if (SceneLoader.IsActiveScene(SceneLoader.Scene.DialogueScene))
Managers.D_MAN.DisplayDialoguePopup();
Managers.U_MAN.DestroyCardPage(true);
Managers.U_MAN.DestroyInteractablePopup(gameObject);
}
}